package com.songlanyun.modules.comment.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.songlanyun.common.drive.RootEntity;
import com.songlanyun.common.enums.CommentConstant;
import com.songlanyun.common.utils.SensiUtil;
import com.songlanyun.modules.account.entity.AccountEntity;
import com.songlanyun.modules.account.entity.UserInfoEntity;
import com.songlanyun.modules.account.service.AccountService;
import com.songlanyun.modules.account.service.UserInfoService;
import com.songlanyun.modules.article.entity.Article;
import com.songlanyun.modules.article.entity.ArticleContent;
import com.songlanyun.modules.article.service.ArticleService;
import com.songlanyun.modules.comment.service.UserCommentService;
import com.songlanyun.modules.opus.entity.Opus;
import com.songlanyun.modules.opus.service.OpusService;
import com.songlanyun.modules.rabbitmq.util.SpringContextUtil;
import lombok.Data;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;

/**
 * 会员的评论
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-30 17:57:16
 */
@Data
@TableName("yxt_user_comment")
public class UserComment extends RootEntity<Long, UserComment> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     *
     */
    @TableId
    private Long id;
    /**
     * 评论人
     */
    private Long commentUserId;

    /**
     * 评论人手机号
     */
    @TableField(exist = false)
    private String mobile;
    /**
     * 评论人名称
     */
    @TableField(exist = false)
    private String nickname;
    /**
     * 评论人头像
     */
    @TableField(exist = false)
    private String headPhoto;

    /**
     *
     */
    @NotBlank(message = "评论内容不能为空")
    @Length(min = 1, max = 200, message = "评论内容长度在1-200个字")
    private String content;

    public boolean filterContent() {
        SensiUtil sensiUtil = (SensiUtil) SpringContextUtil.getBean("sensiUtil");
        String filter = sensiUtil.filter(this.content);
        if (!filter.equals(this.content)) {//有敏感词
            //不显示
            this.content = filter;
            this.isShow = false;
        } else {
            this.isShow = true;
        }
        return this.isShow;
    }

    /**
     * 0'opus' 作品，
     * 1‘article’ 文章，
     * 2‘other’其它
     */
    private CommentConstant.CommentModelType modelType;

    @TableField(exist = false)
    public String title;

    public void loadTitle() {
        switch (modelType) {
            case ARTICLE:
                ArticleService articleService = (ArticleService) SpringContextUtil.getBean("articleService");
                Article article = articleService.getById(this.modelId);
                if (article != null) {
                    this.title = article.getTitle();
                }
                break;
            case OPUS:
                OpusService opusService = (OpusService) SpringContextUtil.getBean("opusService");
                Opus opus = opusService.getById(this.modelId);
                if (opus != null) {
                    this.title = opus.getTitle();
                }
                break;
            default:
        }
    }

    /**
     * 评论的对象
     */
    @NotNull(message = "评论对象不能为空")
    private Long modelId;
    /**
     * 模型是否存在
     */
    private Boolean modelExist;

    /**
     * 上级评论
     */
    private Long parentId;
    /**
     *
     */
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;
    /**
     * 评论的图片
     */
    private String images;
    /**
     * 是否显示
     */
    private Boolean isShow;

    @Override
    public Long getIdentity() {
        return this.getId();
    }
}
